<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="websocket.js"></script>
    <script type="text/javascript">
        jQuery(document).ready(function () {
            //通过POST方法请求，个别浏览器有可能有问题(自行测试)
            httpposttoserver(1, "login", "123");
            //通过Get方法jsonp请求，个别浏览器有可能有问题(自行测试)
            httpgettoserver(1, "login", "123");
            //conn();
        });
        var socket;
        function conn()
        {
            //这是使用证书时使用
             socket = new UDCsocket({
                 ip: 'ws://127.0.0.1', port: 11001, conn: function () {
                     socket.settakon("123123");
                     alert("连接成功");
                     //socket.SendData(1, "login", "123123ssdfsdf", "");
                 }
               , recData: function (text) { 
                   $('#test').append("收到:" + text + '<br/>  ')//这个意思你们都懂了把
               }
               , close: function () { alert("连接关闭"); }
                , error: function (msg) { alert("连接错误" + msg); }
                 , jump: function (ip) { alert("服务器超过最大连接，请连接其他服务器："+ip);}
             });
            //这是不使用证书
             //socket = new UDCsocket({
             //    ip: 'ws://127.0.0.1', port: 8181, conn: function () {
             //        socket.settakon("123123");
             //        alert("连接成功");
             //        socket.SendData(1, "login", "123123ssdfsdf", "");
             //    }
             //   , recData: function (text) {
             //       $('#test').append("收到:" + text + '<br/>  ')//这个意思你们都懂了把
             //   }
             //   , close: function () { alert("连接关闭"); }
             //    , error: function (msg) { alert("连接错误" + msg); }
             //     , jump: function (ip) { alert("服务器超过最大连接，请连接其他服务器：" + ip); }
             //});
        }
        //使用http网关，使用此方法连接。
        function httpposttoserver(command, Request, Root)
        {
            var test = new Object();
            //test.command = 0x2;
            test.Request = Request;//"Send_content";
            if (Root!="")
                test.Root = JSON.stringify(Root);
            else
                test.Root = "";
            test.Parameter ="";
            test.Token = "Token";
            test.Querycount = 0;
            test.Number = '';
            $.ajax({
                type: "post",
                url: "http://127.0.0.1:8080/" + command,
                dataType: "json",
                data: JSON.stringify(test), 
                success: function (data) {
                    alert("返回success:当前人数：" + data["Root"]);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("error:" + errorThrown);
                }
            });
        }
        function httpgettoserver(command, Request, Root) {
            var test = new Object();
            //test.command = 0x2;
            test.Request = Request;//"Send_content";
            if (Root != "")
                test.Root = JSON.stringify(Root);
            else
                test.Root = "";
            test.Parameter = "";
            test.Token = "Token";
            test.Querycount = 0;
            test.Number = '';
            $.ajax({
                type: "get",
                url: "http://127.0.0.1:8080/" + command,
                data: JSON.stringify(test),
                dataType: "jsonp",
                jsonp: 'jsoncallback',
                success: function (data) {
                    alert("success:" +data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("error:" + errorThrown);
                }
            });
        }
        function jsoncallback(m )
        {
            alert("success:" + JSON.stringify(m));
        }
        function SendData()
        {
            socket.settakon("123123");
            socket.SendData(0x01, "senddao", {"Imei":"X7ES6VJMS","Text":"按时收费电视","Command":"1"});
            //  if (socket.takon != "") {
            // socket.settakon("123123");
            //socket.SendData(0x01, "chazhuangtai", 99); //物联网控制传感器状态,4是风扇，5是喷淋，0,2是卷帘正转，1,3反转
         //   socket.SendData(0x03, "QXZ", 41);//获取大棚气候站传感器
          //  socket.SendData(0x01, "open",99, 4)
           // } else { alert("socket is null"); }
        }
        function seestate() {
            //  if (socket.takon != "") {
            socket.settakon("123123");
            socket.SendData(0x01, "senddao", { Imei: "X7ES6VJMS", Text: "按时收费电视", Command: "1" });//获取上下限的条件
            //socket.SendData(0x01, "close", 99, 4)
            // } else { alert("socket is null"); }
        }
    </script>
</head>
<body>
    <button id='ToggleConnection' type="button" onclick='conn();'>
        连接服务器</button><br />
    <br />
    <button id='ToggleConnection' type="button" onclick='SendData();'>
        发送内容：beston</button><br />
    <br />
    <button id='ToggleConnection' type="button" onclick='seestate();'>
        查看状态</button><br />
    <br />
    <div id="test" style="width:1024px"></div>
</body>
</html>
